﻿/*
ParallelPlatform Project - A web platform to distribute and share knowledge
Online Editor - An online tool to create and play animations
Copyright (C) 2011 Daniel Guerrero Martínez & Sergio Rodríguez Lumley

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package Classes{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	public class PreviewExiter extends MovieClip {
		
		private var func:Function;
		
		public function PreviewExiter(ff:Function){
			x 				= 700-30;
			y 				= 500-30;
			func 			= ff;
			this.alpha 		= 0.3;
			buttonMode 		= true;
			useHandCursor	= true;
			this.paint();
			this.addEventListener(MouseEvent.MOUSE_OVER, whenMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, whenMouseOut);
			this.addEventListener(MouseEvent.CLICK, exitPreview);
		}
		
		/**
		  Removes all event listeners from it and it removes from its parent
		*/
		public function remove():void{
			this.removeEventListener(MouseEvent.MOUSE_OVER, whenMouseOver);
			this.removeEventListener(MouseEvent.MOUSE_OUT, whenMouseOut);
			this.removeEventListener(MouseEvent.CLICK, exitPreview);
			parent.removeChild(this);
		}
		
		/******** PRIVATE FUNCTIONS ********/
		
		/**
		  Paints the PreviewExiter
		*/
		private function paint():void{
			graphics.clear();
			graphics.beginFill(0xFFAAAA, 0.3);
			graphics.drawRect(-20, -20, 40, 40);
			graphics.endFill();
			
			graphics.lineStyle(4, 0xAA3333);
			graphics.drawRect(-20, -20, 40, 40);
			graphics.moveTo(-12, -12);
			graphics.lineTo(12, 12);
			graphics.lineStyle(2, 0xAA3333);
			graphics.moveTo(-12, 12);
			graphics.lineTo(12, -12);
		}
		
		private function exitPreview(evt:MouseEvent):void{
			func();
			this.remove();
		}
		
		private function whenMouseOver(evt:MouseEvent):void{
			alpha = 1;
			this.paint();
		}
		
		private function whenMouseOut(evt:MouseEvent):void{
			alpha = 0.4;
			this.paint();
		}
	}
}